package com.chiao.action.admin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.chiao.core.BaseController;
import com.chiao.core.IntEditor;
import com.chiao.entity.admin.Dept;
import com.chiao.entity.admin.ExtGrid;
import com.chiao.service.admin.DeptService;

@Controller
@RequestMapping(value = "/admin/dept")
public class DeptAction extends BaseController {

	@Autowired
	DeptService deptService;
	
	 @InitBinder
	 public void initBinder(WebDataBinder binder) {
		 binder.registerCustomEditor(int.class, new  IntEditor());
	 }
	
	//部门树
	@RequestMapping(value = "/deptTree")
	public @ResponseBody String getDeptTree() {
		return deptService.listDepts();
	}

	//添加部门
	@RequestMapping(value = "/add")
	public @ResponseBody String addDept(Dept d) {
		if (deptService.addDept(d) > 0) {
			return "{success:true}";
		} else {
			return "{failure:true}";
		}
	}
	
	//根据ID列出部门
	@RequestMapping(value="/deptGrid")
	public @ResponseBody ExtGrid<Dept> deptGrid(@RequestParam int id){
		return deptService.listDeptsById(id);
	}
	
	@RequestMapping(value="/delete")
	public @ResponseBody int deleteDeptByIds(@RequestParam String ids){
		return deptService.deleteDeptByIds(ids);
	}
	
	@RequestMapping(value="/update")
	public @ResponseBody String updateDept(Dept dept){
		if (deptService.updateDept(dept) > 0) {
			return "{success:true}";
		} else {
			return "{failure:true}";
		}
	}
	
	
}
